package com.topInterview;

/**
 * @Author: huangzhigao
 * @Date: 2022/2/13 15:04
 */
public class Leetcode10_RegularExpressionMatching {


    public boolean isMatch(String s, String p) {
        if (s == null || p == null) {
            return false;
        }
        char[] schars = s.toCharArray();
        char[] pchars = p.toCharArray();
        if (!isValid(schars, pchars)) {
            return false;
        }
        return true;

    }

    /**
     * 检测字符串是否有效
     *
     * @param schars
     * @param pchars
     * @return
     */
    public boolean isValid(char[] schars, char[] pchars) {
        for (int i = 0; i < schars.length; i++) {
            if (schars[i] == '*' || schars[i] == '.') {
                return false;
            }
        }

        //patter 第一位不能是*，两个*不能在一起
        for (int i = 0; i < pchars.length; i++) {
            if (pchars[i] == '*' && (i == 0 || pchars[i - 1] == '*')) {
                return false;
            }
        }
        return true;

    }
}
